package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    /**
     * 步骤：
     *      1.判断是否进行了预约设置
     *          根据日期查询预约设置对象
     *              如果对象为null， 没有预约设置， 返回不可预约
     *              如果对象不为null, 继续
     *      2. 判断是否预约已满
     *          如果已预约大于等于可预约，预约已满，返回已满
     *          如果已预约小于可预约，继续
     *      3. 判断是否是会员
     *          根据手机号码查询会员对象
     *          如果会员对象为null， 不是会员，自动注册为会员， 继续预约
     *          如果会员对象不为null, 是会员，更新会员信息， 判断是否重复预约
     *              重复预约：同一个人预约了同一天同一个套餐
     *              以：会员id，日期，套餐id 为条件，查询预约信息，如果能查到，重复预约，返回不可重复预约
     *              如果没有查到，继续预约
     *      4. 开始预约
     *          添加预约信息到数据
     *      5. 已预约人数加 1
     *
     */
    @Transactional
    public Result addOrder(Map<String, String> map) {
        log.debug("OrderServiceImpl: addOrder: map ===> " + map);
        String setmealId = map.get("setmealId");
        String idCard = map.get("idCard");
        String sex = map.get("sex");
        String name = map.get("name");
        String telephone = map.get("telephone");
        String orderDateStr = map.get("orderDate");
        String orderType = map.get("orderType");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = null;
        try {
            orderDate = dateFormat.parse(orderDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        log.debug("OrderServiceImpl: addOrder: orderSetting ===> " + orderSetting);
        if (orderSetting==null){
            return new Result(false,"此日期没有预约设置,不可预约");
        }
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            return new Result(false,"预约人数已满");
        }
        Member member = memberDao.findByTelephone(telephone);
        log.debug("OrderServiceImpl: addOrder: member ===> " + member);
        if (member == null){
            member = new Member();
            member.setName(name);
            member.setIdCard(idCard);
            member.setSex(sex);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());

            memberDao.add(member);
        }else {
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setName(name);
            memberDao.edit(member);

            Integer memberId = member.getId();
            log.debug("memberId: " + memberId + "orderDate:" + orderDate + "setmeId:" + setmealId);
            long count = orderDao.findByCondition(memberId,orderDate,setmealId);
            log.debug("OrderServiceImpl: addOrder: count ===> " + count);
            if (count>0){
                return new Result(false,"不可重复预约");
            }
        }
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt(setmealId));

        log.debug("order  ========> " + order);
        int num = orderDao.add(order);
        log.debug("OrderServiceImpl: addOrder: num ===> " + num);
        if (num>0){
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.setReservations(orderSetting);
        }
        return new Result(true, MessageConst.ORDER_SUCCESS,order);
    }

    @Override
    public Map<String, Object> findOrderById(Integer id) {
        log.debug("OrderServiceImpl: findOrderById: id ===> " + id);
        Map<String,Object> map = orderDao.findOrderById(id);
        return map;
    }
}
